package com.yueqian.service.impl;

import com.yueqian.dao.IUserDao;
import com.yueqian.domain.Role;
import com.yueqian.domain.UserInfo;
import com.yueqian.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("userService")
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserDao userDao;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = null;
        try {
            UserInfo userInfo = userDao.findUserByUserName(username);
            System.out.println("userInfo: " + userInfo);
            System.out.println("userInfoRoles: " + userInfo.getRoles());
            List<Role> roles = userInfo.getRoles();
            List<SimpleGrantedAuthority> authorities = getAuthority(roles);
            //构建一个UserDetails的实现类org.springframework.security.core.userdetails.User
//            user = new User(username, "{noop}" + userInfo.getPassword(), authorities);
            user = new User(username, userInfo.getPassword(), authorities);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    private List<SimpleGrantedAuthority> getAuthority(List<Role> roles) {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        for (Role role : roles) {
            authorities.add(new SimpleGrantedAuthority("ROLE_" + role.getRoleName())); // USER   ADMIN
        }
        return authorities;
    }

    @Override
    public List<UserInfo> findAll() throws Exception {
        List<UserInfo> userInfos = userDao.findAll();
        return userInfos;
    }
    //一个数异或两次是它本身   int a = 23    int b = 900  int c =  a^b  int d =  c^b  a==d
    /*
    int a = 23    加密    倒叙后*2
     */

    @Override
    public void save(UserInfo userInfo) throws Exception {
        //对用户输入的密码进行加密,加密方式:MD5 MD5加盐
        String encodePassword = passwordEncoder.encode(userInfo.getPassword());
        userInfo.setPassword(encodePassword);
        //调用持久层
        userDao.save(userInfo);
    }

    @Override
    public UserInfo findById(String id) throws Exception {
        UserInfo userInfo = userDao.findById(id);
        System.out.println("UserServiceImpl:" + userInfo);
        System.out.println("UserServiceImpl:" + userInfo.getRoles());
        System.out.println("UserServiceImpl:" + userInfo.getRoles().get(0).getPermissions());
        return userInfo;
    }

    @Override
    public List<Role> findUserAndNoRole(String id) throws Exception {
        List<Role> roleList = userDao.findUserAndNoRole(id);
        return roleList;
    }

    @Override
    public void addRoleToUser(String userId, String[] ids) throws Exception {
        for (String roleId : ids) {
            userDao.addRoleToUser(userId, roleId);
        }
    }
}
